package algorithms.dynamicProgram;

public class LeetCode_5 {

    public static void main(String[] args) {
//        String s = "babad";
//        String s1 = "cbbd";
        String s2 = "aa";
//        System.out.println(longestPalindrome(s));
//        System.out.println(longestPalindrome(s1));
        System.out.println(longestPalindrome(s2));
    }

    public static String longestPalindrome(String s) {
        int maxLen = 1;
        int startIndex = 0;
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length - 1; i++) {
            for (int j = i + 1; j < chars.length; j++) {
                if(isPalindrome(chars,i,j) && (j-i+1) > maxLen){
                    maxLen = j-i+1;
                    startIndex = i;
                }
            }
        }
        return s.substring(startIndex,startIndex+maxLen);

    }

    public static boolean isPalindrome(char[] chars, int left, int right) {
        while (left < right) {
            if(chars[left] != chars[right]){
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}
